package cn.yhp.mybatis.mapping;

import cn.yhp.mybatis.scripting.LanguageDriver;
import cn.yhp.mybatis.session.Configuration;

import java.util.List;
import java.util.Map;

/**
 * 映射语句类，如映射唯一ID，sql类型，sql内容，入参，出参类型
 * @author yhp
 * @date 2023/11/18
 */
public class MappedStatement {

    private Configuration configuration;

    private String id;

    private SqlCommandType sqlCommandType;

    private SqlSource sqlSource;
    Class<?> resultType;
    private LanguageDriver lang;

    private List<ResultMap> resultMaps;


    MappedStatement(){}

    /**
     * 建造者，构建MappedStatement对象
     */
    public static class Builder{

        private MappedStatement mappedStatement=new MappedStatement();

        public Builder(Configuration configuration,String id,SqlCommandType sqlCommandType,
                       SqlSource sqlSource, Class<?> resultType){
            mappedStatement.configuration=configuration;
            mappedStatement.id=id;
            mappedStatement.sqlCommandType=sqlCommandType;
            mappedStatement.sqlSource=sqlSource;
            mappedStatement.resultType = resultType;
            mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
        }

        public MappedStatement build(){
            assert mappedStatement.configuration!=null;
            assert mappedStatement.id!=null;
            return mappedStatement;
        }

        public String id() {
            return mappedStatement.id;
        }

        public Builder resultMaps(List<ResultMap> resultMaps) {
            mappedStatement.resultMaps = resultMaps;
            return this;
        }

    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public SqlCommandType getSqlCommandType() {
        return sqlCommandType;
    }

    public void setSqlCommandType(SqlCommandType sqlCommandType) {
        this.sqlCommandType = sqlCommandType;
    }

    public SqlSource getSqlSource() {
        return sqlSource;
    }

    public Class<?> getResultType() {
        return resultType;
    }

    public LanguageDriver getLang() {
        return lang;
    }

    public List<ResultMap> getResultMaps() {
        return resultMaps;
    }
}
